﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;

namespace ABC_SA_DAL
{
    public class DBComma
    {
        /// <summary>
        /// Este método funcion para ejecutar comandos de no consulta: Insertar, Modificar y Eliminar
        /// </summary>
        /// <param name="sql"></param>
        public static bool nonSQLCommandEjecutar(string sql) {

            bool ejecucionExitosa;
            Conexion conecta = new Conexion();
            using (SqlConnection conn = conecta.Conectar())
            {
                try {

                    SqlCommand comando = new SqlCommand(sql, conn);
                     if(comando.ExecuteNonQuery() > 0) // Si resultado es mayor a 0, se ejecutó correctamente la sentecia
                         ejecucionExitosa = true;
                     else
                         ejecucionExitosa = false;
                }
                catch (Exception e)
                {
                    string mens = e.ToString();
                    ejecucionExitosa = false;
                }
                finally
                {
                    conn.Close();
                }
                return ejecucionExitosa;
            }

        }

        public static Int32 CommandEjecutarScalar(string sql)
        {

            Int32 id = 0;
            Conexion conecta = new Conexion();
            using (SqlConnection conn = conecta.Conectar())
            {
                try
                {

                    SqlCommand comando = new SqlCommand(sql, conn);
                    id = Convert.ToInt32(comando.ExecuteScalar());

                }
                catch (Exception e)
                {
                    string mens = e.ToString();
                    
                }
                finally
                {
                    conn.Close();
                }
                return id;
            }

        }

        public static SqlDataReader sqlCommandEjecutar(string sql) {

            Conexion conecta = new Conexion();

            using (SqlConnection conn = conecta.Conectar()){

                SqlCommand comando = new SqlCommand(sql, conn);

                SqlDataReader reader = comando.ExecuteReader();

                conn.Close();

                return reader;
            
            }
        }

        public static DataSet sqlCommandDataAdapterEjecutar(string sql)
        {

            Conexion conecta = new Conexion();

            using (SqlConnection conn = conecta.Conectar())
            {
                DataSet ds = new DataSet();
                SqlCommand comando = new SqlCommand(sql, conn);
                SqlDataAdapter adapter = new SqlDataAdapter(comando);
                adapter.Fill(ds);
                conn.Close();

                return ds;

            }
        }


        public static void closeConn() { 
        
        
        }


    }
}
